var prefix = 'notification-message-';

// $("#notification-message-loan");
// $("#notification-message-review");

var success = function(r) {
	alert(r.message);
};
var error = function(err) {
	console.log(err);

};
var ajaxPost = function(url, method, form, success, error) {
	var request = {
		url: url,
		type: method,
		contentType: 'application/json',
		success: function(r) {
			success(r);
		},
		error: function(err) {
			r = {
				success: false,
				data: err
			};
			error(r);
		}
	};
	if (method === 'post') {
		var data = JSON.stringify(form);
		request.data = data;
	}
	$.ajax(request);
};



var postCheck = function() {
	var url = '/center/check_message/';
	var method = 'post';
	var form = {};
	var success = function(r) {
		if (r.success) {
			if (r.message_not_read) {
				if (r.type === 'all') {
					$("#notification-message-loan").attr('style', 'display:inline;');
					$("#notification-message-review").attr('style', 'display:inline;');
					$("#notification-message-distribute").attr('style', 'display:inline;');
				} else {
					var jquery_span = '#' + prefix + r.type;
					$(jquery_span).attr('style', 'display:inline;');
				}
			} else {
				// console.log('no message');
			}
		}; 
	};
	ajaxPost(url, method, form, success, error);
}



var checkMessage = function() {
	postCheck();
}


window.setInterval(checkMessage, 5000)
